// 导入数据库操作模块
import db from '../../mysql/mysql.js'
// token加密模块
import jsonwebtoken from 'jsonwebtoken'
// 导入加密密码模块
import bcryptjs from 'bcryptjs'
// 登录处理函数
export function loginHandler(req, res) {
  const sqlStr = 'select * from user where phone=?'
  db.query(sqlStr, req.body.phone, function (err, result) {
    if(err){
      return res.send(err)
    }
    if (result.length === 0) {
      return res.send({
        status: 1,
        msg: '手机号不存在!',
      })
    }
    if (bcryptjs.compareSync(req.body.password, result[0].password)) {
      // 定义密钥
      const secret = 'xiaoliu123^_^'
      const token = jsonwebtoken.sign({ phone: result[0].phone }, secret, {
        expiresIn: '72H',
      })
      res.send({
        status: 0,
        msg: '登录成功！',
        token: 'Bearer ' + token,
      })
    } else {
      return res.send({
        status: 1,
        msg: '密码错误！',
      })
    }
  })
}
// 用户注册处理函数
export function registerHandlerfn(req, res) {
  if (req.body.phone && req.body.pwd) {
    const sqlstr = 'select * from user where phone=?'
    db.query(sqlstr, req.body.phone, function (err, result) {
      if (err) {
        return res.send(err.message)
      }
      if (result.length) {
        return res.send({
          status: 1,
          msg: '用户名已存在！',
        })
      }
      const password = bcryptjs.hashSync(req.body.pwd, 10)
      const sqlStr = 'insert into user(phone,password) values(?,?)'
      db.query(sqlStr, [req.body.phone, password], function (err, result) {
        if (err) {
          return res.send(err.message)
        }
        if (result.affectedRows === 1) {
          res.send({
            status: 0,
            msg: '注册成功',
          })
        }
      })
    })
  }
}
// 添加购物车
export function addShoppingCar(req, res) {
  const sqlStr =
    'insert into shoppingcar(phone,goodsTitle,goodsImg,price,num,TitlePrice,color,version) values(?,?,?,?,?,?,?,?)'
  db.query(
    sqlStr,
    [
      req.user.phone,
      req.body.title,
      req.body.img,
      req.body.price,
      req.body.goodsNum,
      req.body.goodsNum * req.body.price,
      req.body.color,
      req.body.verison,
    ],
    function (err, result) {
     
      if (err) {
        return res.send({
          status: 1,
          msg: '添加购物车失败！',
        })
      }
      if(result.affectedRows===1){
        res.send({
          status: 0,
          msg: '添加购物车成功！',
        })
      }
      
    }
  )
}
// 获取购物车
export function getShoppingCar(req,res) {
  const sqlStr='select * from shoppingcar where phone=?'
  db.query(sqlStr,req.user.phone,function (err,result) {
    if(err){
      return res.send({
        status:1,
        msg:'购物车获取失败！'
      })
    }
    res.send({
      status:0,
      msg:'购物车获取成功！',
      data:result
    })

  })
}
export function removeGoods(req,res) {
  const sqlStr='DELETE from shoppingcar where id=?'
  db.query(sqlStr,req.body.id,function (err,result) {
    if(err){
      return res.send({
        status:1,
        msg:'删除失败！'
      })
    }
    if(result.affectedRows===1){
      res.send({
        status:0,
        msg:'删除成功'
      })
    }
  })
}
export function updateGoods(req,res) {
  if(req.body.isSelect==='true'){
    req.body.isSelect=1
  }else{
    req.body.isSelect=0
  }
  const sqlStr='UPDATE shoppingcar set num=?,TitlePrice=?,isSelect=? where id=?'
  db.query(sqlStr,[req.body.num,req.body.TitlePrice,req.body.isSelect,req.body.id],function (err,result) { 
    if(err){
      console.log(err.message);
      return res.send({
        status:0,
        msg:'修改失败！'
      })
    }
    if(result.affectedRows===1){
      res.send({
        status:0,
        msg:'修改成功!'
      })
    }
   })
 }